package com.xunk.equipment.context;

/**
 * @ProjectName: xunk
 * @Package: com.xunk.equipment.context
 * @ClassName: RequestHeaderContext
 * @Author: luanzhiwei
 * @Description: 上下文线程本地存储
 * @Date: 2021/4/7 15:42
 * @Version: 1.0
 */
public class RequestHeaderContext {
    private static final ThreadLocal<RequestHeaderContext> REQUEST_HEADER_CONTEXT_THREAD_LOCAL = new ThreadLocal<>();
    /**
     * 用户userid
     */
    private String userId;
    /**
     * 用户token
     */
    private String token;

    public String getUserId() {
        return userId;
    }

    public String getToken() {
        return token;
    }


    public static RequestHeaderContext getInstance() {
        return REQUEST_HEADER_CONTEXT_THREAD_LOCAL.get();
    }

    public void setContext(RequestHeaderContext context) {
        REQUEST_HEADER_CONTEXT_THREAD_LOCAL.set(context);
    }

    public static void clean() {
        REQUEST_HEADER_CONTEXT_THREAD_LOCAL.remove();
    }

    private RequestHeaderContext(RequestHeaderContextBuild requestHeaderContextBuild) {
        this.userId = requestHeaderContextBuild.userId;
        this.token = requestHeaderContextBuild.token;
        setContext(this);
    }

    public static class RequestHeaderContextBuild {
        private String userId;
        private String token;

        public RequestHeaderContextBuild userId(String userId) {
            this.userId = userId;
            return this;
        }

        public RequestHeaderContextBuild token(String token) {
            this.token = token;
            return this;
        }


        public RequestHeaderContext bulid() {
            return new RequestHeaderContext(this);
        }
    }

}
